package com.mdl.springmvc.ioStream.io;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @Desc
 * @Author madengling
 * @Time 2020/12/28 14:59
 */
public class FileWirterTest05 {
    private static final String PATH = "G:\\dhcc\\idea\\MavenProject\\src\\main\\webapp\\files\\";
    public static void main(String[] args) {
        File file = new File(PATH+"a.txt");
        FileWriter fw = null;
        FileReader fr = null;
        try {
            fw = new FileWriter(file,true);
            fw.write("\r");
            fw.write('刷');
            fw.write('新');
            System.out.println("========练习start========");

            fr = new FileReader(PATH+"b.txt");
            copyMethod1(fr,fw);
            fr = new FileReader(PATH+"b.txt");
            copyMethod2(fr,fw);
            System.out.println("========练习end========");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fr.close();
                fw.flush();
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void copyMethod2(FileReader fr, FileWriter fw) throws IOException {
        char chs[] = new char[1024];
        int len;
        while ((len=fr.read(chs)) != -1){
            fw.write(chs,0,len);
        }
        fw.flush();
    }

    private static void copyMethod1(FileReader fr, FileWriter fw) throws IOException {
        int ch;
        while ((ch=fr.read()) != -1){
            fw.write(ch);
        }
        fw.flush();
    }
}
